﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class DetalleProductoRepository : MasterDataAccess, IDetalleProductoRepository
    {
        public List<DETALLE_PRODUCTO> GetAllFromDetalleProductoByProductoId(int id)
        {
            var query = from c in Context.DETALLE_PRODUCTO.Include("MATERIAL")
                        where c.productoId.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<DETALLE_PRODUCTO> GetFromDetalleProductoByMaterialAndProductoId(string desMaterial, int id)
        {
            if (desMaterial != "")
            {
                var query = from c in Context.DETALLE_PRODUCTO.Include("MATERIAL")
                            where (c.MATERIAL.Descripcion.ToUpper().Contains(desMaterial.ToUpper()))
                            & (c.productoId.Equals(id))
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from c in Context.DETALLE_PRODUCTO.Include("MATERIAL")
                            where c.productoId.Equals(id)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
        }

        public DETALLE_PRODUCTO GetFromMaterialByDetalleProductoId(int idmaterial, int idproducto)
        {
            var query = from c in Context.DETALLE_PRODUCTO.Include("PRODUCTO")
                        where c.materialId.Equals(idmaterial) && c.productoId.Equals(idproducto)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void RemoveDetalleProductoByMaterialIdAndProductoId(int idMater, int idProd)
        {
            var existe = Context.DETALLE_PRODUCTO.Where(c => c.productoId.Equals(idProd)
                && c.MATERIAL.Id.Equals(idMater)).SingleOrDefault();
            Context.DETALLE_PRODUCTO.Remove(existe);
            Context.SaveChanges();
        }
    }
}
